//
//  SLLoginViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/10.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLLoginViewController.h"
#import "SLRegisterViewController.h"
#import "SLLoginViewModel.h"



@interface SLLoginViewController ()

@property (nonatomic, strong) UITextField *nameTF;

@property (nonatomic, strong) UITextField *pswTF;

@property (nonatomic, strong) UIButton *forgetBtn;

@property (nonatomic, strong) UIButton *registerBtn;

@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation SLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  backImageView.image = [UIImage imageNamed:@"BG1"];
  [self.view addSubview:backImageView];
  
  [self.view addSubview:self.nameTF];
  [self.view addSubview:self.pswTF];
  [self.view addSubview:self.forgetBtn];
  [self.view addSubview:self.registerBtn];
  [self.view addSubview:self.loginBtn];
    // Do any additional setup after loading the view.
  
  [self addConstrains];
  [self addObservers];
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toHideKeyBoard)];
  [self.view addGestureRecognizer:tapGes];
  if ([DEFAULTS objectForKey:@"lastPhone"] != nil) {
    self.nameTF.text = [DEFAULTS objectForKey:@"lastPhone"];
    [self.nameTF becomeFirstResponder];
    self.pswTF.text = @"";
  }
  
}

- (void)addObservers{
  
  @weakify(self)
  RACSignal *userNameSignal = [self.nameTF.rac_textSignal map:^id(NSString  *value) {
    @strongify(self)
    return @([self isValidUsername:value]);
  }];
  
  RACSignal *passWordSignal = [self.pswTF.rac_textSignal map:^id(NSString *value) {
    @strongify(self);
    return @([self isValidPassword:value]);
  }];
  
  RACSignal *bothSignal = [RACSignal combineLatest:@[userNameSignal,passWordSignal] reduce:^id(NSNumber *usernameValid, NSNumber *passwordValid){
    return @([usernameValid boolValue]&&[passwordValid boolValue]);
  }];
  
  [bothSignal subscribeNext:^(id x) {
    @strongify(self);
    if ([x boolValue]) {
      self.loginBtn.enabled = YES;
    }else{
      self.loginBtn.enabled = NO;
    }
  }];
  
  [RACObserve(self.loginBtn, enabled) subscribeNext:^(NSNumber *x) {
    @strongify(self);
    if ([x boolValue]) {
      [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      self.loginBtn.backgroundColor = SLYellowColor;
    }else{
      [self.loginBtn setTitleColor:SLInkColor forState:UIControlStateNormal];
      self.loginBtn.backgroundColor = SLGrayColor;
    }
  }];
  
}

#pragma mark - 对点击按钮的限制
- (BOOL)isValidUsername:(NSString *)username {
  // 当输入正确的手机号位数
  return [SLLoginViewModel checkTelNumber:username];
}

- (BOOL)isValidPassword:(NSString *)password {
  // 当输入正确的验证码位数
  return password.length > 5 && password.length < 16;
}

- (void)addConstrains{
  [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.mas_topLayoutGuideBottom).offset(36);
    make.centerX.equalTo(self.view);
    make.width.equalTo(self.view).offset(-50);
    make.height.equalTo(@30);
  }];
  
  UIView *nameLine = [UIView lineView];
  [self.view addSubview:nameLine];
  [nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.equalTo(self.nameTF);
    make.top.equalTo(self.nameTF.mas_bottom).offset(15);
    make.height.equalTo(@1);
  }];
  
  [self.pswTF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.nameTF.mas_bottom).offset(36);
    make.centerX.equalTo(self.view);
    make.width.equalTo(self.view).offset(-50);
    make.height.equalTo(@30);
  }];
  
  UIView *pswLine = [UIView lineView];
  [self.view addSubview:pswLine];
  [pswLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.equalTo(self.pswTF);
    make.top.equalTo(self.pswTF.mas_bottom).offset(15);
    make.height.equalTo(@1);
  }];
  
  [self.forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(pswLine.mas_bottom).offset(70);
    make.left.equalTo(self.nameTF);
    make.size.mas_equalTo(CGSizeMake(70, 20));
  }];
  
  [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.forgetBtn);
    make.right.equalTo(self.nameTF);
    make.size.equalTo(self.forgetBtn);
  }];
  
  [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@46);
    make.left.right.equalTo(self.nameTF);
    make.top.equalTo(self.registerBtn.mas_bottom).offset(30);
  }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private Method

- (void)toRegister{
  SLRegisterViewController *registerVC = [[SLRegisterViewController alloc] initWithControllerType:ControllerTypeRegister];
  [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)toChangePsw{
  SLRegisterViewController *registerVC = [[SLRegisterViewController alloc] initWithControllerType:ControllerTypeForget];
  [self.navigationController pushViewController:registerVC animated:YES];

}

- (void)toHideKeyBoard{
  [self.view endEditing:YES];
}


- (void)toLogin{
  [DEFAULTS setObject:self.nameTF.text forKey:@"lastPhone"];
  NSDictionary *dic = @{@"phone":self.nameTF.text,@"psw":self.pswTF.text};
  [SLLoginViewModel loginWithParameters:dic];
}



#pragma mark - getter

- (UITextField *)pswTF{
  if (!_pswTF) {
    _pswTF = [UITextField new];
    _pswTF.placeholder = @"密码";
    [_pswTF setValue:SLGrayColor forKeyPath:@"_placeholderLabel.textColor"];
    [_pswTF setValue:[UIFont boldSystemFontOfSize:22] forKeyPath:@"_placeholderLabel.font"];
    _pswTF.secureTextEntry = YES;
    
  }
  return _pswTF;
}

- (UITextField *)nameTF{
  if (!_nameTF) {
    _nameTF = [UITextField new];
    _nameTF.placeholder = @"手机";
  
    _nameTF.keyboardType = UIKeyboardTypeNumberPad;
    [_nameTF setValue:SLGrayColor forKeyPath:@"_placeholderLabel.textColor"];
    [_nameTF setValue:[UIFont boldSystemFontOfSize:22] forKeyPath:@"_placeholderLabel.font"];
    
  }
  return _nameTF;
}

- (UIButton *)forgetBtn{
  if (!_forgetBtn) {
    _forgetBtn = [UIButton buttonWithTitle:@"忘记密码"];
    [_forgetBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _forgetBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [_forgetBtn addTarget:self action:@selector(toChangePsw) forControlEvents:UIControlEventTouchUpInside];
  }
  return _forgetBtn;
}

- (UIButton *)registerBtn{
  if (!_registerBtn) {
    _registerBtn = [UIButton buttonWithTitle:@"我要注册"];
    [_registerBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _registerBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [_registerBtn addTarget:self action:@selector(toRegister) forControlEvents:UIControlEventTouchUpInside];
  }
  return _registerBtn;
}

- (UIButton *)loginBtn{
  if (!_loginBtn) {
    _loginBtn = [UIButton buttonWithTitle:@"登录"];
    [_loginBtn setTitleColor:SLInkColor forState:UIControlStateNormal];
    _loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    [_loginBtn addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn.enabled = NO;
  }
  return _loginBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
